<template>
	<button v-if="haveAuth" class="slot-btn" type="default" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" withCredentials="true" hover-class="none">
		<slot></slot>
	</button>
	<view v-else @click="nextClick">
		<slot></slot>
	</view>
</template>

<script>
import {updateUserinfo,getUserInfo} from '../../tool/userTool'
	let app = getApp();
	export default {
		name: 'wx-auth',
		props: {
			haveAuth: {
				type: Boolean,
				default(){
					return false
				}
			},
			callBack: {
				type: String,
				default(){
					return ""
				}
			}
		},

		computed:{

		},
		data() {
			return {
				authflag: app.globalData.authflag
			};
		},
		methods: {

			getPhoneNumber(e){
				let anthOk = false;
				if(!e.mp.detail.encryptedData){
					uni.showToast({
						title: '请授权手机号',
						icon: "none"
					})
					uni.$emit(this.callBack,{anthOk:anthOk})
				}else{
					let wxUserInfo = e.mp.detail;
					updateUserinfo({
						params:{
							encrypted_data: wxUserInfo.encryptedData,
							iv: wxUserInfo.iv
						}
					}).then(res => {
						// app.globalData.phoneNum = "15517589670";
						// uni.setStorageSync('phoneNum', "15517589670");
						// anthOk = true;
						getUserInfo().then(res => {
							if(res.phone) {
								app.globalData.phoneNum = res.phone;
								uni.setStorageSync('phoneNum', res.phone);
								anthOk = true;
								uni.$emit(this.callBack,{anthOk:anthOk})
							}else{
								uni.$emit(this.callBack,{anthOk:anthOk})
							}
						})
					})

					// getUserInfo().then(res => {
					// 	if(res.phone) {
					// 		app.globalData.phoneNum = "15517589670";
					// 		uni.setStorageSync('phoneNum', "15517589670");
					// 		anthOk = true;
					// 	}
					// })

					// uni.$emit(this.callBack,{anthOk:anthOk})
				}

			},
			nextClick(){
				uni.$emit(this.callBack,{anthOk:false})
			}

		}
	}
</script>

<style lang="scss">
.slot-btn{
		background-color: rgba(0,0,0,0)!important;
		padding: 0;margin: 0;

		position: none;
		display: block;

		&::after {
			border: none;


		}
		&.button-hover {
			color:rgba(0, 0, 0, 0.6);
			background-color:#fff;
			transform: translate(1rpx, 1rpx);
		}
}
</style>
